﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebBanThuoc.DTO;
using System.Data.SqlClient;

namespace WebBanThuoc.DAO
{
    public class TraLoiBaiDang_DAO
    {
        private SqlConnection connection;
        //public bool hasData { set; get; }
        public TraLoiBaiDang_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private TraLoiBaiDang getBy(string whereCom, string orderCom)
        {
            TraLoiBaiDang data = null;
            string comStr = "Select * from [TraLoiBaiDang]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    data = new TraLoiBaiDang();
                    dap.DaReader.Read();

                    data.MaCauHoi = Convert.ToInt32(dap.DaReader["MaCauHoi"]);
                    data.SoThuTu = Convert.ToInt32(dap.DaReader["SoThuTu"]);
                    data.IDKhachHang = Convert.ToInt32(dap.DaReader["IDKhachHang"]);
                    data.NoiDung = dap.DaReader["NoiDung"].ToString();
                    data.NgayTraLoi = Convert.ToDateTime(dap.DaReader["NgayTraLoi"]);
                    data.UserAdmin = dap.DaReader["UserAdmin"].ToString();                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public TraLoiBaiDang getByKey(string MaCauHoi)
        {
            return getBy(" [MaCauHoi]=" + MaCauHoi, " [MaCauHoi] asc");
        }

        public int createSoTuThu(int MaCauHoi)
        {
            int result = 1;
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, "SELECT [dbo].[fnc_CreateNew_SoThuTu_TraLoiBaiDang] (" + MaCauHoi + ") as col");
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    dap.DaReader.Read();
                    result = Convert.ToInt32(dap.DaReader["col"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private int update(TraLoiBaiDang obj, string whereCom)
        {
            string comQuery = "UPDATE [TraLoiBaiDang] SET [MaCauHoi]=@MaCauHoi, [SoThuTu]=@SoThuTu, [IDKhachHang]=@IDKhachHang"
                + ", [NoiDung]=@NoiDung, [NgayTraLoi]=@NgayTraLoi, [UserAdmin]=@UserAdmin ";

            if (whereCom.Trim() != "")
                comQuery += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.AddParameters("@MaCauHoi", obj.MaCauHoi, System.Data.SqlDbType.Int);
                dap.AddParameters("@SoThuTu", obj.SoThuTu, System.Data.SqlDbType.Int);
                dap.AddParameters("@IDKhachHang", obj.IDKhachHang, System.Data.SqlDbType.Int);
                dap.AddParameters("@NoiDung", obj.NoiDung, System.Data.SqlDbType.NText);
                dap.AddParameters("@NgayTraLoi", obj.NgayTraLoi, System.Data.SqlDbType.SmallDateTime);
                dap.AddParameters("@UserAdmin", obj.UserAdmin, System.Data.SqlDbType.NVarChar);

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int add(TraLoiBaiDang obj)
        {
           
            string comQuery = "INSERT INTO [TraLoiBaiDang] ([MaCauHoi],[SoThuTu],[IDKhachHang]"
                + ",[NoiDung],[NgayTraLoi],[UserAdmin])"
                + " VALUES (@MaCauHoi,@SoThuTu,@IDKhachHang,@NoiDung,@NgayTraLoi,@UserAdmin)";

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.AddParameters("@MaCauHoi", obj.MaCauHoi, System.Data.SqlDbType.Int);
                dap.AddParameters("@SoThuTu", obj.SoThuTu, System.Data.SqlDbType.Int);
                dap.AddParameters("@IDKhachHang", obj.IDKhachHang, System.Data.SqlDbType.Int);
                dap.AddParameters("@NoiDung", obj.NoiDung, System.Data.SqlDbType.NText);
                dap.AddParameters("@NgayTraLoi", obj.NgayTraLoi, System.Data.SqlDbType.SmallDateTime);
                dap.AddParameters("@UserAdmin", obj.UserAdmin, System.Data.SqlDbType.NVarChar);
                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        public List<TraLoiBaiDang> getAll()
        {
            List<TraLoiBaiDang> data = new List<TraLoiBaiDang>();
            string comStr = "Select * from [TraLoiBaiDang] order by [MaCauHoi]";
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        TraLoiBaiDang obj = new TraLoiBaiDang();

                        obj.MaCauHoi = Convert.ToInt32(dap.DaReader["MaCauHoi"]);
                        obj.SoThuTu = Convert.ToInt32(dap.DaReader["SoThuTu"]);
                        obj.NoiDung = dap.DaReader["NoiDung"].ToString();
                        obj.NgayTraLoi = Convert.ToDateTime(dap.DaReader["NgayTraLoi"]);
                        obj.UserAdmin = dap.DaReader["UserAdmin"].ToString();
                        data.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }
    }
}